package com.example.content_provider_server.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

public class UserDBHelper extends SQLiteOpenHelper {
    static private final String DB_NAME = "user.db";
    static public final String TABLE_NAME = "user_info";
    static private final int DB_VERSION = 1;
    private static UserDBHelper instance = null;

    // 私有构造器，单例模式
    private UserDBHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    // 获取实例
    public static UserDBHelper getInstance(@Nullable Context context) {
        Log.d("zhoutao", "UserDBHelper.getInstance");
        if (instance == null) {
            instance = new UserDBHelper(context);
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Log.d("zhoutao", "onCreate: create database table");
        // 创建数据库，执行建表语句
        String sql = "CREATE table if not exists " + TABLE_NAME + " (" +
                "_id integer primary key autoincrement not null," +
                "name varchar," +
                "age integer," +
                "height long," +
                "weight float," +
                "married integer" +
                ");";
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        String addPhoneFields = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN phone varchar";
        sqLiteDatabase.execSQL(addPhoneFields);

        String addPasswordFields = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN password varchar";
        sqLiteDatabase.execSQL(addPasswordFields);
    }
}
